/** 
 * 第一阶段 9.25日前                   1
 * 第二阶段  9.25日 - 19：30前          2
 * 第三阶段  9.25日19：30 - 直播结束      3
 * 第四阶段  9.25日结束后                  4
 */
$(function() {
 var phased = 1;
 var canStarRain = true;
 var rainPlay= false;
 
 if (getQueryVariable('phased')){
	 phased = getQueryVariable('phased');
 }
 setPhased();
	
	
function setPhased() {
	console.log(phased)
	$('.body').removeClass('phased1');
	$('.body').removeClass('phased2');
	$('.body').removeClass('phased3');
	$('.body').removeClass('phased4');
	$('.body').addClass('phased' + phased);
}
	
	// 获取珍探编号
	var identifierNum;
	if(localStorage.getItem('identifier')){
		
		identifierNum = localStorage.getItem('identifier');
		console.log(identifierNum)
		setNum(identifierNum);
		$('.video_btn3').fadeIn(0);
		$('.video_btn2').fadeOut(0);
	}else{
		//请求接口获取珍探编号
		// 如果未注册，隐藏所有珍探编号
		$.ajax({
			url:'https://h5.jtsummit.cn/jingling2009/index/initlogin',
			method:'get',
			dataType:'json',
			data:{},
			success:function(res){
				console.log(res)
				if (res.code == '0000'){//已有珍探编号
					var userNum = res.data.user_info.num;
					if(userNum != ''){
						localStorage.setItem('identifier', userNum);
						setNum(identifierNum);
						$('.video_btn3').fadeIn(0);
						$('.video_btn2').fadeOut(0);
					}else{
						$('.prize_step1_num_wrap').fadeOut(0);
						$('.no_wrap').fadeOut(0);
						$('.video_btn2').fadeIn(0);
						$('.video_btn3').fadeOut(0);
					}
				}else if(res.code == '0001'){//没有编号
					$('.prize_step1_num_wrap').fadeOut(0);
					$('.no_wrap').fadeOut(0);
					$('.video_btn2').fadeIn(0);
					$('.video_btn3').fadeOut(0);
				}else{
					$('.prize_step1_num_wrap').fadeOut(0);
					$('.no_wrap').fadeOut(0);
					$('.video_btn2').fadeIn(0);
					$('.video_btn3').fadeOut(0);
					alert(res.msg);
				}
			}
		})
	}
	var palyering = true;
	$('.video_btn1').click(function(){
		if(palyering){
			$('.player video')[0].pause()
		} else{
			$('.player video')[0].play()
		}
		palyering = !palyering;
	})
	// 预览图片
	var previewSwiper;
	$('.swiper_cover').click(function(){
		var ind = $(this).attr('data-index');
		console.log(ind)
		initPreview(ind);
		
	});
	
	function initPreview(num){
		if(num == undefined){
			num = 0;
		}
		$('.preview').fadeIn(0);
		previewSwiper = new Swiper('.preview-container', {
			initialSlide:num
		});
	}
	$('.close_preview').click(function(){
		$('.preview').fadeOut(0);
	})
	
	
	// 每隔10秒请求判断页面状态
	var checkStageInter;
	
	// checkStageInter = setInterval(function(){
		
	// },10000);
	checkStatus()
	function checkStatus(){
		$.ajax({
			url:'https://h5.jtsummit.cn/jingling2009/luck/isRound',
			methdo:'get',
			dataType:'json',
			data:{},
			success:function(res){
				console.log(res)
				if (res.code == '0000'){
					if(res.round == 0){
						canStarRain = false;
					}else if(res.round == 4){
						stopRed();
						canStarRain = false;
					}else{
						sessionStorage.setItem('redRound',res.round);
					}
				}
			}
		})
	}
	
	$('.video_btn2').click(function(){
		$('.register_wrap').fadeIn(0);
	});
	$('.close_rule_btn').click(function(){
		$('.rule_wrap').fadeOut(0);
	})
	$('.close_model_btn').click(function(){
		$('.model_wrap').fadeOut(0);
	});
	// 注册
	var isselect = true;
	var globalTel = '';
	var interVal;
	var smsNum = 60;
	$('.register_checkbox').click(function(){
		if($(this).attr('data-select') != 1){ //勾选
			isselect = true;
			$(this).attr('src','https://cdnjinlingguan.influencee.cn/img/register/checkbox_selcted.png');
			$(this).attr('data-select','1');
		} else {
			isselect = false;
			$(this).attr('src','https://cdnjinlingguan.influencee.cn/img/register/checkbox.png');
			$(this).attr('data-select','0');
		}
	});
	// 获取验证码
	$('.getcode').click(function(){
		var canclick = $(this).attr('data-click');
		if (canclick == 0){
			var tel = $('.tel_input').val();
			if(tel == '' || tel == undefined){
				alert('请输入手机号');
				return false
			}
			if (tel.length != 11){
				alert('请输入正确的手机号');
				return false
			}
			$('.getcode').attr('data-click','1');
			setSmsBtn();
			$.ajax({
				url: 'https://h5.jtsummit.cn/jingling2009/info/smscode',
				method:'post',
				dataType:'json',
				data:{
					phone:tel
				},
				success:function(res){
					console.log(res)
					if(res.code == '0000'){
						globalTel = tel;
						alert('短信发送成功');
					}else{
						alert(res.msg);
					}
				}
			})
		}
		
	})
	function setSmsBtn(){
		clearInterval(interVal);
		interVal = setInterval(function(){
			smsNum--;
			if(smsNum <= 0){
				smsNum = 60;
				clearInterval(interVal);
				$('.getcode').text('获取验证码');
				$('.getcode').attr('data-click','0');
			}else{
				$('.getcode').text(smsNum);
			}
		},1000);
	}
	// 提交
	$('.register_submit').click(function(){
		console.log(isselect)
		var smsCode = $('.msg_input').val();
		if(smsCode == '' || smsCode == undefined){
			alert('请输入验证码');
			return false
		}
		if(!isselect){
			alert('请勾选服务条款');
			return false
		}
		$.ajax({
			url:'https://h5.jtsummit.cn/jingling2009/info/set',
			method:'post',
			dataType:'json',
			data:{
				phone:globalTel,
				smscode: smsCode
			},
			success:function(res){
				if(res.code == '0000'){
					var identifier = res.data.num;
					localStorage.setItem('identifier',identifier);
					createPoster(identifier);
					$('.register_wrap').fadeOut(0);
					$('.model_wrap').fadeIn(0);
				}else{
					alert(res.msg)
				}
			}
		})
	})
	
	var application, pixiapp;

	function startRed() {
		if (canStarRain && !rainPlay){
			rainPlay = true;
			$('.canvas_wrap').fadeIn(0);
			var canvas = document.getElementById('render');
			application = new PIXI.Application({
				width: document.body.offsetWidth,
				height: document.body.offsetHeight,
				backgroundColor: 0x000000,
				view: canvas,
				antialias: true,
				transparent:true,
				clearBeforeRender: true
			});
			var stage = application.stage;
			var wid = document.body.offsetWidth;
			var hei = document.body.offsetHeight;
			pixiapp = new PixiApp({
				width: wid,
				height: hei,
				stage: stage,
				PIXI: PIXI,
				app: application
			});
			pixiapp.init();
			
		}
	}

	function stopRed() {
		if (canStarRain && rainPlay){
			pixiapp.stop();
		}
	}
	// startRed()
	
	// 开始红包雨
	$('.video_btn1').click(function(){
		
		startRed();
		// 结束红包雨
		setTimeout(function(){
			$('.canvas_wrap').fadeOut(0);
			stopRed();
		},6000);
	});
	// 去注册
	$('.video_btn2').click(function(){
		$('.register_wrap bg_hundered').fadeIn(0);
	});
	$('.register_back').click(function(){
		$('.register_wrap').fadeOut(0);
	})
	$('#door').on('ended', function() {
		$('.start').fadeOut(0);
		$('.body').fadeIn(0);
		initSwipers();
		initPlayer();
	});

	$('.video_play').click(function() {
		$(this).fadeOut(0);
		$('#door')[0].play();
	});

	function initSwipers() {
		if (phased == 3 || phased == 4){
			var mySwiper = new Swiper('.swiper-container', {
				direction: 'horizontal',
				// initialSlide:1,
				on: {
					slideChangeTransitionStart: function() {
						console.log(this.activeIndex);
					},
				},
			});
		}
	}
	$('.video_btn3').click(function(){
		$('.register_wrap').fadeIn(0);
	})
	// var zhibo='http://live.wfsidao.com/app/first.m3u8?auth_key=1566741955-0-0-e4c754f779a9b71625701de5ec186edc'
	// var yure='https://live201908.oss-cn-beijing.aliyuncs.com/video/%E6%A0%BC%E5%BC%8F%E5%B7%A5%E5%8E%82%E7%A5%9D%E8%B4%BA%E8%A7%86%E9%A2%91%2B%E6%BA%AF%E6%BA%90%E8%A7%86%E9%A2%91.mp4?v=12'
	function initPlayer() {
		var player = new Aliplayer({
			"id": "player-con",
			//宣传视频
			//		  "source": " https://live201908.oss-cn-beijing.aliyuncs.com/video/%E6%A0%BC%E5%BC%8F%E5%B7%A5%E5%8E%82%E7%A5%9D%E8%B4%BA%E8%A7%86%E9%A2%91%2B%E6%BA%AF%E6%BA%90%E8%A7%86%E9%A2%91.mp4?v=12",
			//直播链接
			"source": "https://live201908.oss-cn-beijing.aliyuncs.com/video/20190805_3.mp4",
			"width": "6.32rem",
			"height": "3.57rem",
			"autoplay": true,
			"isLive": true,
			"rePlay": false,
			"playsinline": true,
			"preload": true,
			"controlBarVisibility": "hover",
			"useH5Prism": true
		}, function(player) {
			console.log("The player is created");
		});
	}
	// 设置珍探编号
	function setNum(num){
		var numStr = String(num);
		$('.prize_step1_num_number').remove();
		$('.num_text').remove();
		for(var i = 0;i<numStr.length;i++){
			var html1 = '<img src="https://cdnjinlingguan.influencee.cn/img/' + numStr[i] + '.png" alt="" class="prize_step1_num_number">';
			$('.prize_step1_num_number_wrap').append(html1);
			var html2 = '<img src="https://cdnjinlingguan.influencee.cn/img/' + numStr[i] + '.png" alt="" class="num_text">';
			$('.number_wrap').append(html2);
		}
	}
	// createPoster(12345678);
	// 合成海报
	function createPoster(num){
		// 创建画布
		var canvas = document.createElement('canvas');
		// 绘制文字环境
		var context = canvas.getContext('2d');
		//设置画布倍数
		var getPixelRatio = function(context) {
		        var backingStore = context.backingStorePixelRatio ||
		            context.webkitBackingStorePixelRatio ||
		            context.mozBackingStorePixelRatio ||
		            context.msBackingStorePixelRatio ||
		            context.oBackingStorePixelRatio ||
		            context.backingStorePixelRatio || 1;
		    
		        return (window.devicePixelRatio || 1) / backingStore;
		    };
		 
		var scal = getPixelRatio(context);
		// 画布宽度
		canvas.width = 750*scal;
		// 画布高度
		canvas.height = 1334*scal;
		//清除canvas原有内容
		context.rect(0, 0, canvas.width, canvas.height);
		// 设置字体
		context.font =(30*scal)+ 'px bold fzcy';
		// 设置字体颜色
		context.fillStyle = '#ffe770';
		//设置海报底图
		var model = new Image();
		model.crossOrigin = "Anonymous";
		model.src = 'https://cdnjinlingguan.influencee.cn/img/register/model.jpg';
		model.onload = function(){
			context.drawImage(model, 0, 0, canvas.width, canvas.height);
			//渲染昵称
			context.fillText(num, 444*scal, 758*scal);
			
			var base64 = canvas.toDataURL("image/png");
			$('.model_cover').attr('src', base64);
		}
	}
	

	function getQueryVariable(variable){
	   var query = window.location.search.substring(1);
	   var vars = query.split("&");
	   for (var i=0;i<vars.length;i++) {
			   var pair = vars[i].split("=");
			   if(pair[0] == variable){return pair[1];}
	   }
	   return(false);
	}
})
